package com.ensah.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.boudaa.dao.exceptions.EntityNotFoundException;
import com.ensah.dao.ClasseDao;
import com.ensah.dao.EtudiantDao;
import com.ensah.dao.MatiereDao;
import com.ensah.dao.RoleDao;
import com.ensah.metier.bo.Classe;
import com.ensah.metier.bo.Etudiant;
import com.ensah.metier.bo.Matiere;
import com.ensah.metier.bo.Role;
import com.ensah.service.ClasseService;

public class ClasseServiceImpl implements ClasseService{

	private ClasseDao clDao;
	
	public ClasseDao getClDao() {
		return clDao;
	}




	public void setClDao(ClasseDao clDao) {
		this.clDao = clDao;
	}


	
	public Classe saveClasse(Classe cl) {
		
		return clDao.create(cl);
	}
	

	public void delete(Long id) {
		clDao.delete(id);
		
	}


	
	public void update(Classe cl) {
        clDao.update(cl);		
	}
	
	public List<Classe> getAll() {
			
			return clDao.getAll();
		}


	public Classe getClasseById(Long id) throws EntityNotFoundException {
		
		return clDao.findById(id);
	}

	
	
	
	public List<Etudiant> getEtdClasse(Long idClasse) throws EntityNotFoundException {

	List<Etudiant> etds=clDao.rechercheEtdClasse(idClasse);

		return etds;
	}


	
	public Classe getClassebyNom(String nomClasse) throws EntityNotFoundException {
		
		Classe cl=clDao.getClassebyNom(nomClasse);
		return cl;
		
	}


	public List<Matiere> rechercheMatiereClasse(Long idClasse) throws EntityNotFoundException {
		return clDao.rechercheMatiereClasse(idClasse);
	}
	
	public List<String> getClassesName(){
		List<Classe> classes=clDao.getAll();
		List<String > nomClasses=new ArrayList<String>();
		for(Classe it:classes){
			nomClasses.add(it.getNom());
		}
		return nomClasses;
	}
  public List<String> getMatieresName(Long idClasse) throws EntityNotFoundException{
	    List<Matiere> matieres=this.rechercheMatiereClasse(idClasse);
		List<String > nomMatiere=new ArrayList<String>();

	  for(Matiere it:matieres){
		  nomMatiere.add(it.getNom());
	  }
	return nomMatiere;  
  }






public static void main(String[] args) throws EntityNotFoundException {
	
	ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
 	;
	ClasseDao cldao=(ClasseDao) ctx.getBean("clasDao");
	MatiereDao matDao=(MatiereDao) ctx.getBean("MatDao");
	EtudiantDao etdao=(EtudiantDao) ctx.getBean("etddaoid");
	 RoleDao roledao= (RoleDao) ctx.getBean("roledaoid");
	 
	List<Matiere> mats=new ArrayList<Matiere>();
 	Matiere mat=new Matiere("mat1", "Java", 40,null);
 	Matiere mat2=new Matiere("mat1", "c++", 40,null);
     Matiere mat3=new Matiere("mat3", "p/sql", 50,null);
     mats.add(mat);
     mats.add(mat2);
     mats.add(mat3);
     matDao.create(mat);
     matDao.create(mat2);
     matDao.create(mat3);
     Role role=new Role("etudiant");
     roledao.create(role);
 List<Etudiant> etds=new ArrayList<Etudiant>();
   Etudiant  etd=new Etudiant("El boujddaini", "farida","R44944","hkgkg","06555555","en@hotmail.fr",new Date(),"farida","farida",role,true,true,true,true,98,"66666666",null,null,null);
   Etudiant  etd2=new Etudiant("ben amar", "hafsa","R44444","hkgkg","06555555","en@hotmail.fr",new Date(),"farida","farida",role,true,true,true,true,9,"66666666",null,null,null);
   Etudiant  etd3=new Etudiant("Aoulad Alloch", "saloua","R44444","hkgkg","06555555","en@hotmail.fr",new Date(),"farida","farida",role,true,true,true,true,99,"66666666",null,null,null);
   Etudiant  etd4=new Etudiant("Achatbi", "Iman","R44444","hkgkg","06555555","en@hotmail.fr",new Date(),"farida","farida",role,true,true,true,true,88,"66666666",null,null,null);
   etdao.create(etd);
   etdao.create(etd2);
   etdao.create(etd3);
   etdao.create(etd4);
   etds.add(etd);
   etds.add(etd2);
   etds.add(etd3);
   etds.add(etd4);
   Classe c =new Classe("GI2","c1",etds,mats);
   cldao.create(c);
 etd=  etdao.findById(etd.getIdUtil());
 etd.setClasse(c);
 etdao.update(etd);
	
}






}
